package com.zgr.struts.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.orm.hibernate5.support.OpenSessionInViewFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

public class HelloWorld extends SuperAction {

    private static final long serialVersionUID = -1862730489352392884L;

    public String message;
    @Override
    public String execute() throws Exception {
        System.out.println("测试execute()");
        message = "Hello World, Struts2";
        return SUCCESS;
    }

    public String login(){
        try {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            String username = request.getParameter("username");
            String password = request.getParameter("pwd");
            message = "Hello World, Struts2";
            System.out.println("测试 message=" + message);
            System.out.println("username=" + username);
            System.out.println("password=" + password);
            if ("aa".equals(username) && "123456".equals(password)) {
                return SUCCESS;
            } else {
                return "login";
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    public String getMessage() {
        System.out.println("测试getMessage()");
        System.out.println(message);
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
